From 1364b6275beb6d6647dafd3a7037cccbc2817a76 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Fri, 13 May 2016 14:16:39 -0400 Subject: [PATCH] Incorporate "using crates from crates.io" into the main guide Connects to rust-lang/cargo#1035. Using crates from crates.io is a common thing that many people will want to know how to do right away and deserves to be part of the main guide. --- src/doc/crates-io.md | 34 ++-------------------- src/doc/guide.md | 68 ++++++++++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 60 deletions(-) diff --git a/src/doc/crates-io.md b/src/doc/crates-io.md index 7281c31c5..6914938a9 100644 --- a/src/doc/crates-io.md +++ b/src/doc/crates-io.md @@ -1,35 +1,3 @@ -% Cargo and crates.io - -In addition to using dependencies from git repositories (as mentioned in -[the guide](guide.html)) Cargo can also publish to and download from the -[crates.io][crates-io] central repository. This site serves as a location to -discover and download packages, and `cargo` is configured to use it by default -to find requested packages. - -The guide will explain how crates can use crates.io through the `cargo` command -line tool. - -[crates-io]: https://crates.io/ - -# Using crates.io-based crates - -The method of specifying a dependency on a crate from crates.io is slightly -different than the method of specifying a dependency on a git repository. The -syntax for doing so is: - -```toml -[dependencies] -glob = "0.0.3" -``` - -With this format, adding new dependencies should just add a new line, you don’t -need to add `[dependencies]` for each dependency listed, for example: - -```toml -[dependencies] -glob = "0.0.3" -num = "0.0.4" -``` The string value for each key in this table is a [semver][semver] version requirement. @@ -311,3 +279,5 @@ explicitly whitelisted crates.io querying the org in question by pressing the “Grant Access” button next to its name: ![Authentication Access Control](images/auth-level-acl.png) + +[crates-io]: https://crates.io/ \ No newline at end of file diff --git a/src/doc/guide.md b/src/doc/guide.md index f714a3956..d3c79c5da 100644 --- a/src/doc/guide.md +++ b/src/doc/guide.md @@ -127,21 +127,35 @@ To build, use `cargo build`: This will fetch all of the dependencies and then build them, along with the project. -# Adding Dependencies +# Adding Dependencies from crates.io -To depend on a library, add it to your `Cargo.toml`. +[crates.io][crates-io] is the Rust community's central repository that serves +as a location to discover and download packages. `cargo` is configured to use +it by default to find requested packages. + +To depend on a library hosted on [crates.io], add it to your `Cargo.toml`. + +[crates-io]: https://crates.io/ ## Adding a dependency -It’s quite simple to add a dependency. Simply add it to your `Cargo.toml` file: +If your `Cargo.toml` doesn't already have a `[dependencies]` section, add that, +then list the crate name and version that you would like to use. This example +adds a dependency of the `time` crate: ```toml [dependencies] time = "0.1.12" ``` -Re-run `cargo build` to download the dependencies and build your source with the new dependencies. +The version string is a [semver][semver] version requirement. + +[semver]: https://github.com/steveklabnik/semver#requirements +If we also wanted to add a dependency on the `regex` crate, we would not need +to add `[dependencies]` for each crate listed. Here's what your whole +`Cargo.toml` file would look like with dependencies on the `time` and `regex` +crates: ```toml [package] @@ -150,27 +164,12 @@ version = "0.1.0" authors = ["Your Name "] [dependencies] +time = "0.1.12" regex = "0.1.41" ``` -You added the `regex` library, which provides support for regular expressions. - -Now, you can pull in that library using `extern crate` in -`main.rs`. - -``` -extern crate regex; - -use regex::Regex; - -fn main() { - let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap(); - println!("Did our date match? {}", re.is_match("2014-01-01")); -} -``` - -The next time we build, Cargo will fetch this new dependency, all of its -dependencies, compile them all, and update the `Cargo.lock`: +Re-run `cargo build`, and Cargo will fetch the new dependencies and all of +their dependencies, compile them all, and update the `Cargo.lock`:
$ cargo build
     Updating registry `https://github.com/rust-lang/crates.io-index`
@@ -188,18 +187,31 @@ dependencies, compile them all, and update the `Cargo.lock`:
    Compiling regex v0.1.41
    Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)
-Run it: - -
$ cargo run
-     Running `target/hello_world`
-Did our date match? true
- Our `Cargo.lock` contains the exact information about which revision of all of these dependencies we used. Now, if `regex` gets updated, we will still build with the same revision until we choose to `cargo update`. +You can now use the `regex` library using `extern crate` in `main.rs`. + +``` +extern crate regex; + +use regex::Regex; + +fn main() { + let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap(); + println!("Did our date match? {}", re.is_match("2014-01-01")); +} +``` + +Running it will show: + +
$ cargo run
+     Running `target/hello_world`
+Did our date match? true
+ # Project Layout Cargo uses conventions for file placement to make it easy to dive into a new -- 2.30.2